// pages/home/vote/components/signup/signup.js
const app = getApp();
var { _apply_vote_activity, _update_userinfo } = require('../../../../../utils/xiaohuangyu')
const validate = require('../../../../../utils/validate')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    activityId: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    vote_apply: {
      name: '',
      mobile: '',
      introduce: ''
    },
    imgList: [],
    imgIdList: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    bindGetUserInfo(res) {
      if (!res.detail.userInfo) {
        return
      }
      _update_userinfo(res.detail.userInfo, reqs => {
        if (reqs.status) {
          app.globalData.userInfo = reqs.data
          // 提交数据
          this.submitTap()
        }
      })
    },
    submitTap() {
      let vote_apply = this.data.vote_apply
      vote_apply['images'] = this.data.imgIdList
      vote_apply['activity_id'] = this.data.activityId
      if (validate.isEmpty(vote_apply['name'])) {
        validate.showToast('参赛名不能为空')
        return false
      }
      if (validate.isEmpty(vote_apply['mobile'])) {
        validate.showToast('参赛手机号不能为空')
        return false
      }
      if (validate.isEmpty(vote_apply['introduce']) || vote_apply['introduce'].length > 200) {
        validate.showToast('请输入1-200字以内的问价介绍')
        return false
      }
      if (vote_apply['images'].length < 1) {
        validate.showToast('请至少上传一张图片')
        return false
      }
      // 报名
      _apply_vote_activity(vote_apply, res => {
        console.log('_apply_vote_activity', res)
        if (res.status) {
          // 向父组件传值，设置底部导航的 报名和我的参数设置
          this.triggerEvent('successVoteApply', {
            params: {
              number: res.data,
            }
          })
        }
      })
    },
    nameInputBlur(res) {
      this.setData({
        [`vote_apply.name`]: res.detail.value
      })
    },
    mobileInputBlur(res) {
      this.setData({
        [`vote_apply.mobile`]: res.detail.value
      })
    },
    introduceBindInput(res) {
      this.setData({
        [`vote_apply.introduce`]: res.detail.value
      })
    },
    // 选择图片
    ChooseImage() {
      const that = this
      wx.chooseImage({
        count: 4, //默认9
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], //从相册选择
        success: (res) => {

          const tempFilePaths = res.tempFilePaths
          for (let i = 0; i < tempFilePaths.length; i++) {
            app.uploadFile(tempFilePaths[i], 'vote_image', reqs => {
              const result = JSON.parse(reqs)
              that.setData({
                imgList: that.data.imgList.concat([tempFilePaths[i]]),
                imgIdList: that.data.imgIdList.concat([result.data.id])
              })
            })
          }
        }
      });
    },
    // 查看图片
    ViewImage(e) {
      wx.previewImage({
        urls: this.data.imgList,
        current: e.currentTarget.dataset.url
      });
    },
    // 删除图片
    DelImg(e) {
      this.data.imgList.splice(e.currentTarget.dataset.index, 1);
      this.data.imgIdList.splice(e.currentTarget.dataset.index, 1);
      this.setData({
        imgList: this.data.imgList,
        imgIdList: this.data.imgIdList
      })
    },
  }
})
